package com.atxiao.controller;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.XML;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.web.client.RestTemplate;
import org.w3c.dom.Document;

import java.util.HashMap;

/**
 * Created by 李潇潇 on 2021 09-27
 * 微信：2501902696
 * desc: 模版消息推送模拟
 */
@RestController
@Slf4j
public class WXController {
    /*
     * 微信测试账号推送
     * */
    @GetMapping("/push")
    public void push() {
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx77bb69292323a000");
        wxStorage.setSecret("29bd368145806115ad6820133e62806e");
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("o5kho6DgC7SDry8zCmXuvHJGvrgI")//要推送的用户openid
                .templateId("Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg")//模版id
                .url("https://30paotui.com/")//点击模版消息要访问的网址
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
        //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        try {
            String res = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println(res);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }

    @RequestMapping("/getAccessToken")
    public String getAccessToken(){
//        String appId="wx7941309898adae7f";
//        String appSecret="132ac9b83fe08fd7370631cafd4bae83";


        String appId="wx8d881aa5d3205519";
        String appSecret="aa93ed32061c9f6bde02999cefa9fe3b";

        String destUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&" +
                "appid="+appId+"&" +
                "secret="+appSecret;
        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject(destUrl, String.class);
        System.out.println(res);
//        JSONObject jsonObject = new JSONObject();
        JSONObject object = JSONObject.parseObject(res);
        String access_token = (String) object.get("access_token");
        System.out.println("获取到的access_token为："+access_token);
        return access_token;
    }

    //        ----------------------------消息推送----------------------
    @GetMapping("/getMsg")
    public String getMsg(@RequestBody JSONObject param){

        HashMap<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("touser","oMP2I5wUFC43iKMEZDxGOo9LiytA");
        bodyMap.put("template_id","UuWMGJ5fKLhQ5bZnRMU9Oxl9R5_vp47bDOObhuiU_cs");
        bodyMap.put("url","http://xiaostudy.top");
        bodyMap.put("topcolor","#FF0000");
        HashMap<String, Object> data = new HashMap<>();
        HashMap<String, Object> first = new HashMap<>();
        first.put("value","有一条缴费记录");
        first.put("color","#173177");


        data.put("first",first);

        HashMap<String, Object> keyword1 = new HashMap<>();
        keyword1.put("value","978.90");
        keyword1.put("color","#173177");

        data.put("keyword1",keyword1);

        HashMap<String, Object> keyword2 = new HashMap<>();
        keyword2.put("value","10月21日");
        keyword2.put("color","#173177");

        data.put("keyword2",keyword2);
        bodyMap.put("data",data);
        System.out.println("map:"+bodyMap);

        String res1 = net.sf.json.JSONObject.fromObject(bodyMap).toString();
        System.out.println(res1);


//        HashMap<String, String> map1 = new HashMap<>();
//        HashMap<String, String> map2 = new HashMap<>();
//        map2.put("k1","v1");
//        map1.put("map2",map2);

        System.out.println("请求参数："+param);
        String accessTolenUrl="http://127.0.0.1:9090/getAccessToken";
        String msgUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" ;
        RestTemplate restTemplate = new RestTemplate();
        String access_token = restTemplate.getForObject(accessTolenUrl, String.class);
        System.out.println("获取到的AccessToken："+access_token);
//        JSONObject jsonObject = restTemplate.postForObject(msgUrl + access_token, param, JSONObject.class);
        JSONObject jsonObject = restTemplate.postForObject(msgUrl + access_token, bodyMap, JSONObject.class);
        String res = jsonObject.toJSONString();
        System.out.println("返回结果："+res);
        return res;
    }



    @RequestMapping("/subAppid")
    public String subAppID(@RequestBody JSONObject param){
        HashMap<String, Object> data = new HashMap<>();
        data.put("appid",param.get("APPID"));
        data.put("mch_id",param.get("MCH_ID"));
        data.put("sub_mch_id",param.get("SUB_MCH_ID"));
        data.put("sub_appid",param.get("SUB_APP_ID"));
        data.put("sign",param.get("E7DF9A69C68A7342D7B7D98AAD291B6A"));
        String res = XmlUtil.mapToXmlStr(data);

        Document xml = XmlUtil.mapToXml(data, "xml");
        log.info("转换成的xml:"+xml);
//        System.out.println(xml.toString());


//        String url="https://api.mch.weixin.qq.com/secapi/mch/addsubdevconfig";
        String url="https://101.91.0.140:443/secapi/mch/addsubdevconfig";
        RestTemplate restTemplate = new RestTemplate();

        XML xmlObject = restTemplate.postForObject(url, res, XML.class);

//        System.out.println(jsonObject.toJSONString());

//        return jsonObject.toJSONString();

        return "";




    }






}
